<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

const LKL_URL = 'https://124.74.143.162:15023/thirdpartplatform/';
const LKL_KEY = 'wxd9c866ad31c3c6wxd9c866ad31c3c6';

class LklController extends Controller{
    //新增商户进件
    public function addMch(Request $request){
        $data = $request->all();
        $url = LKL_URL.'merchmanage/6001.dor';
        $attachments = $toxml = array();
        $data['funCod'] = '6001';
        $data['compOrgCode'] = 'YZSM';
        $data['threeOrgCode'] = '';
        $data['posType'] = 'WECHAT_PAY';
        $data['orderNo'] = '201708111818';
        $data['settlePeriod'] = '';
        $data['wechatFees']['wechatType'] = 'WECHAT_PAY_FEE';    //微信支付手续费
        $data['termNum'] = 1;
        $data['termType'] = 'S90';
        $data['retUrl'] = 'http:www.yizhi.com';
        $termInfos['devSerialNo'] = '123456';
        $termInfos['devTypeName'] = '网络POS';
        $termInfos['termVer'] = 'S90';
        $data['termInfos'] = $termInfos;
        $data['feeAssumeType'] = '';
        $data['feeAssume'] = '';
        $attachments[0]['attachmentName'] = '';
        $attachments[0]['attachmentKind'] = '';
        $attachments[0]['attachmentType'] = 'ID_CARD_FRONT';       //身份证正面
        $data['attachments'] = $attachments[0];
        $data['MAC'] = sha1($data['compOrgCode'].$data['channelType'].$data['merLicenseNo'].$data['provinceCode'].$data['cityCode'].$data['countyCode'].$data['crLicenceNo']
                .$data['contactMobile'].$data['openningBankNo'].$data['clearingBankNo'].$data['accountNo'].$data['mccCode'].$data['termNum'].LKL_KEY);

        $data['aliFees']['wechatType'] = 'ALIPAY_WALLET_FEE';    //支付宝钱包手续费
        $ali['wechatFees'] = $data['aliFees'];
        unset($data['aliFees']);
        
        $attachments[1]['attachmentName'] = '';
        $attachments[1]['attachmentKind'] = '';
        $attachments[1]['attachmentType'] = 'ID_CARD_BEHIND';      //身份证反面
        
        $attachments[2]['attachmentName'] = '';
        $attachments[2]['attachmentKind'] = '';
        $attachments[2]['attachmentType'] = 'BANK_CARD';      //银行卡
        
        $attachments[3]['attachmentName'] = '';
        $attachments[3]['attachmentKind'] = '';
        $attachments[3]['attachmentType'] = 'OTHERS';      //协议
        
        $attachments[4]['attachmentName'] = '';
        $attachments[4]['attachmentKind'] = '';
        $attachments[4]['attachmentType'] = 'BUSINESS_LICENCE';      //营业执照
        
        $behand['attachments'] = $attachments[1];
        $bank['attachments'] = $attachments[2];
        $others['attachments'] = $attachments[3];
        $str = $this->arrayToXMLString($data).$this->arrayToXMLString($ali).$this->arrayToXMLString($behand).$this->arrayToXMLString($bank).$this->arrayToXMLString($others);
        if($data['channelType']=='TP_MERCHANT'){
            $license['attachments'] = $attachments[4];
            $str.=$this->arrayToXMLString($license);
        }

        $xmldata = "<?xml version='1.0' encoding='GBK'?><ROOT>".$str."</ROOT>";
//        echo $xmldata;exit;
        $header[] = 'Content-type: application/xml';
        $xmldata=iconv("UTF-8", "GBK", $xmldata);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);    //请求Https时需要这两行配置
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);
        $req = curl_exec($ch);
        curl_close($ch);

        $xml = simplexml_load_string($req,NULL,LIBXML_NOCDATA);
//        echo json_encode($xml);exit;
        $arr = json_decode(json_encode($xml), TRUE);
        $ret = array(
            '返回码'    => $arr['responseCode'],
            '返回信息'  => $arr['message'],
            '唯一标识'  => $arr['orderNo']
        );
        if($ret['返回码']=='000000'){
            $ret['进件编号'] = $arr['contractId'];
        }
        $html = "<html>"
                . "<head>"
                . "<title>进件结果</title>"
                . "<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>"
                . "</head>"
                . "<body>";
        foreach ($ret as $key => $value){
            $html.="<label>".$key."：</label>";
            $html.=$value."<br/>";
        }
        $html.="</body>"
                . "</html>";
        echo $html;
        exit;
    }
    
    //查询商户号等
    public function searchShopNo(){
        $url = LKL_URL.'merchmanage/6005.dor';
        $data = $toxml = array();
        $data['FunCod'] = '6005';
        $data['compOrgCode'] = 'YZSM';
        $data['contractId'] = '9029360';
        $data['MAC'] = sha1($data['compOrgCode'].$data['contractId'].LKL_KEY);
        $toxml['ROOT'] = $data;
        $ret = "<?xml version='1.0' encoding='GBK'?>".$this->arrayToXMLString($toxml);
    //    echo $ret;exit;
        $header[] = 'Content-type: application/xml';
        
    //    $xmldata=iconv("UTF-8", "GBK", $xmldata);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);    //请求Https时需要这两行配置
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        // post的变量
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $ret);
    //    var_dump(curl_error($ch));
    //    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        $req = curl_exec($ch);
        curl_close($ch);
    //    $str=iconv("GBK", "UTF-8", $req);
    //    echo $req;exit;
        $xml = simplexml_load_string($req,NULL,LIBXML_NOCDATA);
//        echo json_encode($xml);
        $xml = json_decode(json_encode($xml),true);
        var_dump($xml);
    }
    
    //上传附件
    public function uploadEnclosure(Request $request){
        $url = 'https://nb.lakala.com:8080/UploadAttachment';
        $data = $toxml = array();
//        $data = $request->all();
        $data['version'] = '10';
        $data['command'] = 'ICP_UPLOAD_ATTACHMENT';
        $data['platform'] = 'TP';
        $data['compOrgCode'] = '377080';
        $data['contractId'] = $request->request->get('contractId');
        $data['type'] = $request->request->get('type');
        $data['shopNo'] = $request->request->get('shopNo');
        $temp = '';
        if($request->hasFile('pic')){
            $file = $request->file('pic');
            if($file->getSize()>=5*1024*1024){
                echo '图片大小不能超过5M';exit;
            }
            $temp = file_get_contents($file);
//            dd($temp);
            $data['pic'] = $temp;
        }
        $header[] = 'Content-Type: multipart/form-data; boundary=----Curl17YZSMFileUpload';
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);    //请求Https时需要这两行配置
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        // post的变量
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $req = curl_exec($ch);
        curl_close($ch);
    //    $str=iconv("GBK", "UTF-8", $req);
        echo $req;exit;
    }
    
    //查询银行信息
    public function searchBankMsg(Request $request){
        $url = LKL_URL.'merchmanage/6004.dor';
        $data = $toxml = array();
        $data['FunCod'] = '6004';
        $data['compOrgCode'] = 'YZSM';
        $data['districtCode'] = '';
        $data['bankName'] = $request->bankName;
        $data['openingBank'] = '';      //开户支行和开户行号必须一个有值
        $data['MAC'] = sha1($data['compOrgCode'].$data['districtCode'].$data['openingBank'].LKL_KEY);
        $toxml['ROOT'] = $data;
        $ret = "<?xml version='1.0' encoding='GBK'?>". $this->arrayToXMLString($toxml);
    //    echo $ret;exit;
        $header[] = 'Content-type: application/xml';

        $ret=iconv("UTF-8", "GBK", $ret);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);    //请求Https时需要这两行配置
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        // post的变量
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $ret);
    //    var_dump(curl_error($ch));
    //    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        $req = curl_exec($ch);
        curl_close($ch);
    //    var_dump($req);exit;
    //    $str=iconv("GBK", "UTF-8", $req);
        $xml = simplexml_load_string($req,NULL,LIBXML_NOCDATA);
//        echo json_encode($xml);
        $xml = json_decode(json_encode($xml),true);
        $html = "<html>"
                . "<head>"
                . "<title>银行信息</title>"
                . "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>"
                . "</head>"
                . "<body>"
                . "<table>"
                . "<tr><th>支行名称</th><th>开户行</th><th>清算行</th></tr>";
        foreach ($xml['banks'] as $banks){
            $html.="<tr><td>".$banks['bankName']."</td>";
            $html.="<td>".$banks['openingBank']."</td>";
            $html.="<td>".$banks['clearingBank']."</td></tr>";
        }
        $html.="</table>"
                . "</body>"
                . "</html>";
        echo $html;exit;
    }
    
    //数组转xml字符串
    private function arrayToXMLString ($array) {
        $xmlString = "";

        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $xmlString .= "<$key>\n";
                $xmlString .= $this->arrayToXMLString($value);
                $xmlString .= "</$key>\n";
            } else {
                $xmlString .= "<$key>";
                $xmlString .= "$value";
                $xmlString .= "</$key>\n";
            }
        }

        return $xmlString;
    }
    
}

